package com.akupilih.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.afree.data.time.Day;
import org.afree.data.time.TimeSeries;
import org.afree.data.time.TimeSeriesCollection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.akupilih.app.chart.TrendChartView;
import com.akupilih.app.entity.E_Chart;
import com.akupilih.app.helper.JSONHelper;
import com.akupilih.app.helper.VolleyHelper;
import com.akupilih.app.model.WebService;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

public class TrendChartFragment extends Fragment {
	private String URL;
	private static final String TAG = "TRENDCHART";
	private List<E_Chart> listchart;
	private FragmentActivity myContext;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		URL = WebService.POLITICAL_WAVE();
		TrendChartView trend = getData(getActivity(), URL);

		return trend;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		myContext = (FragmentActivity) activity;

	}

	private TrendChartView getData(final Context contex, String URL) {
		final TimeSeriesCollection dataset = new TimeSeriesCollection();
		JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
				URL, null, new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject obj) {

						try {
							JSONObject root = obj.getJSONObject("mediawave");
							JSONArray chart = root.getJSONArray("chart");
							JSONObject objChart = chart.getJSONObject(0);
							JSONArray data = objChart.getJSONArray("data");
							listchart = JSONHelper.getTimeChart(data);
							SimpleDateFormat standardDateFormat = new SimpleDateFormat(
									"yyyy-MM-dd");
							TimeSeries s1 = new TimeSeries("Prabowo - Hatta");
							TimeSeries s2 = new TimeSeries(
									"Joko Widodo - Jusuf Kala");
							// inserting to dataset
							if (!listchart.isEmpty()) {
								for (int i = 0; i < listchart.size(); i++) {
									try {
										Date myDate = standardDateFormat
												.parse(listchart.get(i)
														.getTrend_tgl());
										s1.add(new Day(myDate), listchart
												.get(i).getTrend_buzz1());
										s2.add(new Day(myDate), listchart
												.get(i).getTrend_buzz2());
									} catch (ParseException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}

								}
							}

							dataset.addSeries(s1);
							dataset.addSeries(s2);

						} catch (JSONException e) {
							ACRAPostSender.TRACE(TAG + " ERROR", "getData : "
									+ e.getMessage());
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						// TODO Auto-generated method stub
						ACRAPostSender.TRACE(TAG
								+ ".getData(...).new ErrorListener() {...}",
								"onErrorResponse : " + error.toString());
						VolleyHelper.showVolleyError(contex, error);
					}
				});
		request.setRetryPolicy(new DefaultRetryPolicy(9000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		BaseApplication.getInstance().addToRequestQueue(request, TAG);
		TrendChartView trends = new TrendChartView(contex, dataset);
		return trends;

	}
}
